#include "OpenCV.h"

#include <Vfw.h>

COpenCV::COpenCV() :
    m_Camera(NULL),
    m_GrayImage(NULL), m_BGRAImage(NULL), m_FlippedImage(NULL), original(NULL)
{
}

COpenCV::~COpenCV()
{
    StopCapture();
}

/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////

bool COpenCV::StartCapture(int width, int height)
{
    StopCapture();

    m_Camera = cvCreateCameraCapture(0);

    if(IsCaptureStarted() == false)
    {
        return false;
    }

    cvSetCaptureProperty(m_Camera, CV_CAP_PROP_FRAME_WIDTH, width);
    cvSetCaptureProperty(m_Camera, CV_CAP_PROP_FRAME_HEIGHT, height);

    if(GetImageSizes(width, height) == false)
    {
        return false;
    }

    IplImage *image = cvQueryFrame(m_Camera);

    CvSize imageSize = cvGetSize(image);

    m_GrayImage				= cvCreateImage(imageSize, image->depth, 1);
    m_GrayImage->origin		= image->origin;

    m_BGRAImage				= cvCreateImage(imageSize, 8, 4);
    m_BGRAImage->origin		= image->origin;

    m_FlippedImage			= cvCreateImage(imageSize, 8, 3);
    m_FlippedImage->origin	= image->origin;

    original                            = cvCreateImage(imageSize, 8, 3);
    original->origin            = image->origin;

    return true;
}

bool COpenCV::StopCapture()
{
    if(m_Camera == NULL)
    {
        return false;
    }

    if(m_Camera != NULL)
    {
        cvReleaseCapture(&m_Camera);
        m_Camera = NULL;
    }

    if(m_GrayImage != NULL)
    {
        cvReleaseImage(&m_GrayImage);
        m_GrayImage = NULL;
    }

    if(m_BGRAImage != NULL)
    {
        cvReleaseImage(&m_BGRAImage);
        m_BGRAImage = NULL;
    }

    if(m_FlippedImage != NULL)
    {
        cvReleaseImage(&m_FlippedImage);
        m_FlippedImage = NULL;
    }

    if(original != NULL)
    {
        cvReleaseImage(&original);
        original = NULL;
    }

    return true;
}

bool COpenCV::GetImageSizes(int &width, int &height) const
{
    if(IsCaptureStarted() == false)
    {
        return false;
    }

    IplImage *cvimage = cvQueryFrame(m_Camera);

    width	= cvimage->width;
    height	= cvimage->height;

    return true;
}

/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////

IplImage* COpenCV::QueryFrame()
{
    if(IsCaptureStarted() == false)
    {
        return NULL;
    }

    IplImage *image = cvQueryFrame(m_Camera);
    original = image;

    cvCvtColor(image, m_FlippedImage, CV_BGR2RGB);
    cvFlip(m_FlippedImage);
    cvCvtColor(m_FlippedImage, m_GrayImage, CV_BGR2GRAY);
    cvCvtColor(m_GrayImage, m_BGRAImage, CV_GRAY2BGRA);

    return m_FlippedImage;
}

IplImage* COpenCV::GetGrayImage()
{
    return m_GrayImage;
}

IplImage* COpenCV::GetBGRAImage()
{
    return m_BGRAImage;
}
IplImage* COpenCV::GetImage()
{
    return original;
}
